package com.sh.data.engine.repository.dao.normalization.standard;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sh.data.engine.repository.dao.normalization.standard.arg.DictionaryMappingDetailArg;
import com.sh.data.engine.repository.dao.normalization.standard.entity.DictionaryMappingDetailEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

/**
 * @author jingchen.hr
 */
@Repository
public interface DictionaryMappingDetailMapper extends BaseMapper<DictionaryMappingDetailEntity> {

    /**
     * 根据映射id查详情列表
     *
     * @param dictId
     * @param mapSource
     * @return
     */
    default List<DictionaryMappingDetailEntity> queryList(Long dictId, Integer mapSource) {
        LambdaQueryWrapper<DictionaryMappingDetailEntity> query =
            new QueryWrapper<DictionaryMappingDetailEntity>().lambda();
        query
            .eq(DictionaryMappingDetailEntity::getRowState, 1)
            .eq(DictionaryMappingDetailEntity::getDictId, dictId)
            .eq(DictionaryMappingDetailEntity::getMapSource, mapSource)
            .orderByDesc(DictionaryMappingDetailEntity::getUpdateTime);

        return this.selectList(query);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    default DictionaryMappingDetailEntity getById(Long id) {

        LambdaQueryWrapper<DictionaryMappingDetailEntity> query =
            new QueryWrapper<DictionaryMappingDetailEntity>().lambda();
        query
            .eq(DictionaryMappingDetailEntity::getRowState, 1)
            .eq(DictionaryMappingDetailEntity::getId, id);
        return this.selectOne(query);
    }

    /**
     * 根据tblId查询
     *
     * @param tblId
     * @return
     */
    default List<DictionaryMappingDetailEntity> getListByTblTd(Long tblId) {

        LambdaQueryWrapper<DictionaryMappingDetailEntity> query =
            new QueryWrapper<DictionaryMappingDetailEntity>().lambda();
        query
            .eq(DictionaryMappingDetailEntity::getRowState, 1)
            .eq(DictionaryMappingDetailEntity::getTblId, tblId);
        return this.selectList(query);
    }

    default int deleteByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return 0;
        }

        LambdaQueryWrapper<DictionaryMappingDetailEntity> query =
            new QueryWrapper<DictionaryMappingDetailEntity>().lambda();
        query.in(DictionaryMappingDetailEntity::getId, ids);
        return this.delete(query);
    }

    default int deleteByArgs(DictionaryMappingDetailArg arg) {
        if (Objects.isNull(arg.getMapSource())) {
            return 0;
        }
        LambdaQueryWrapper<DictionaryMappingDetailEntity> query =
            new QueryWrapper<DictionaryMappingDetailEntity>().lambda();
        query.eq(DictionaryMappingDetailEntity::getMapSource, arg.getMapSource());
        return this.delete(query);
    }

    default List<DictionaryMappingDetailEntity> getDetailByTblIds(List<Long> tblIds) {

        LambdaQueryWrapper<DictionaryMappingDetailEntity> query =
            new QueryWrapper<DictionaryMappingDetailEntity>().lambda();
        query.in(DictionaryMappingDetailEntity::getTblId, tblIds);

        return this.selectList(query);
    }

    default List<DictionaryMappingDetailEntity> getDetailByProjectAndMppingId(
        Long projectId, Long dictMappingId, Long tblId) {

        LambdaQueryWrapper<DictionaryMappingDetailEntity> query =
            new QueryWrapper<DictionaryMappingDetailEntity>().lambda();
        query
            .eq(DictionaryMappingDetailEntity::getProjectId, projectId)
            .eq(DictionaryMappingDetailEntity::getDictMappingId, dictMappingId)
            .eq(DictionaryMappingDetailEntity::getRowState, 1)
            .eq(DictionaryMappingDetailEntity::getTblId, tblId);

        return this.selectList(query);
    }
}
